function calculateProgressColor(percentage) {
    if (percentage < 0 || percentage > 1) {
        return "rgb(0,0,0)";
    }

    // position, R, G, B
    // position must be in strictly increasing order
    const colorInfo = [
        [0, 255, 0, 0],
        [0.5, 255, 186, 0],
        [1, 56, 255, 93]
    ];

    for (let i = colorInfo.length - 2; i >= 0; i--) {
        if (colorInfo[i][0] <= percentage) {
            const sectionPercentage =
                (percentage - colorInfo[i][0]) / (colorInfo[i + 1][0] - colorInfo[i][0]);

            let targetColor = [];

            for (let j = 1; j <= 3; j++){
                targetColor[j - 1] =
                    colorInfo[i][j] + sectionPercentage * (colorInfo[i + 1][j] - colorInfo[i][j]);
            }
            
            return `rgb(${targetColor.join(",")})`;
        }
    }

    return "rgb(0,0,0)";
}

function isMobileBrowser(){
    const uaLowerCase = navigator.userAgent.toLowerCase();
    return uaLowerCase.includes("android")
        || uaLowerCase.includes("harmony")
        || uaLowerCase.includes("iphone")
        || uaLowerCase.includes("mobile");
}

export { calculateProgressColor, isMobileBrowser };